#include "vertex_types.fxh"
#include "Samplers.fxh"
#include "Functions.fxh"
		
	float g_Bump=10;
		
//**NORMAL TEXTURED VERTEX**//
TNORMAL_TEXTURED_VERTEX_PS RenderNormalTexturedVS(TNORMAL_TEXTURED_VERTEX_VS IN)
{
	TNORMAL_TEXTURED_VERTEX_PS OUT= (TNORMAL_TEXTURED_VERTEX_PS) 0;
	
	
	OUT.WorldPosition	= mul(float4(IN.Position,1.0)	,g_WorldMatrix);
	OUT.HPosition 		= mul(float4(IN.Position,1.0)	,g_WorldViewProjectionMatrix);
	OUT.WorldNormal		= mul(IN.Normal		,g_WorldMatrix);
	OUT.UV 				= IN.UV;
	
	return OUT;
}

float4 RenderNormalTexturedPS(TNORMAL_TEXTURED_VERTEX_PS IN) : COLOR
{
	float4 l_DiffuseColor = tex2D(S0LinearSampler,IN.UV);	
	return CalcLighting(IN.WorldPosition.xyz,IN.WorldNormal.xyz,l_DiffuseColor);
}

technique tecDefaultNormalTexturedVertex
{
    pass p0
    {        
        VertexShader =compile vs_3_0 RenderNormalTexturedVS();
        PixelShader = compile ps_3_0 RenderNormalTexturedPS();
    }
}
//*****************************************************************************************


//**NORMAL COLORED VERTEX**//
TNORMAL_COLORED_VERTEX_PS RenderNormalColoredVS(TNORMAL_COLORED_VERTEX_VS IN)
{
	TNORMAL_COLORED_VERTEX_PS OUT= (TNORMAL_COLORED_VERTEX_PS) 0;
	
	
	OUT.WorldPosition	= mul(float4(IN.Position,1.0)	,g_WorldMatrix);
	OUT.HPosition 		= mul(float4(IN.Position,1.0)	,g_WorldViewProjectionMatrix);
	OUT.WorldNormal		= mul(IN.Normal		,g_WorldMatrix);
	OUT.Color 			= IN.Color;
	
	return OUT;
}

float4 RenderNormalColoredPS(TNORMAL_COLORED_VERTEX_PS IN) : COLOR
{
	float4 l_DiffuseColor = IN.Color;	
	return CalcLighting(IN.WorldPosition.xyz,IN.WorldNormal.xyz,l_DiffuseColor);
}

technique tecDefaultNormalColoredVertex
{
    pass p0
    {        
        VertexShader =compile vs_3_0 RenderNormalColoredVS();
        PixelShader = compile ps_3_0 RenderNormalColoredPS();
    }
}
//*****************************************************************************************


//**NORMAL 2TEXTURED VERTEX**//
TNORMAL_2TEXTURED_VERTEX_PS RenderNormal2TexturedVS(TNORMAL_2TEXTURED_VERTEX_VS IN)
{
	TNORMAL_2TEXTURED_VERTEX_PS OUT= (TNORMAL_2TEXTURED_VERTEX_PS) 0;
	
	
	OUT.WorldPosition	= mul(float4(IN.Position,1.0)	,g_WorldMatrix);
	OUT.HPosition 		= mul(float4(IN.Position,1.0)	,g_WorldViewProjectionMatrix);
	OUT.WorldNormal		= mul(IN.Normal		,g_WorldMatrix);
	OUT.UV 				= IN.UV;
	OUT.UV2 			= IN.UV2;
	
	return OUT;
}

float4 RenderNormal2TexturedPS(TNORMAL_2TEXTURED_VERTEX_PS IN) : COLOR
{
	float4 l_DiffuseColor = tex2D(S0LinearSampler,IN.UV);
	float4 l_LightMap = tex2D(S1LinearSampler,IN.UV2);	
	l_DiffuseColor=l_DiffuseColor*l_LightMap;
	return CalcLighting(IN.WorldPosition.xyz,IN.WorldNormal.xyz,l_DiffuseColor);
}

technique tecDefaultNormal2TexturedVertex
{
    pass p0
    {        
        VertexShader =compile vs_3_0 RenderNormal2TexturedVS();
        PixelShader = compile ps_3_0 RenderNormal2TexturedPS();
    }
}
//*****************************************************************************************


//**NORMAL TAN BIN TEXTURED VERTEX**//
TNORMAL_TANGENT_BINORMAL_TEXTURED_VERTEX_PS RenderNormalTanBinTexturedVS(TNORMAL_TANGENT_BINORMAL_TEXTURED_VERTEX_VS IN)
{
	TNORMAL_TANGENT_BINORMAL_TEXTURED_VERTEX_PS OUT= (TNORMAL_TANGENT_BINORMAL_TEXTURED_VERTEX_PS) 0;
	
	
	OUT.WorldPosition	= mul(float4(IN.Position,1.0)	,g_WorldMatrix);
	OUT.HPosition 		= mul(float4(IN.Position,1.0)	,g_WorldViewProjectionMatrix);
	OUT.WorldNormal		= mul(IN.Normal		,g_WorldMatrix);
	OUT.UV 				= IN.UV;
	
	OUT.WorldTangent	= mul(IN.Tangent,g_WorldMatrix);
	OUT.WorldBinormal 	= mul(cross(IN.Tangent,IN.Normal),g_WorldMatrix);
	
	return OUT;
}

float4 RenderNormalTanBinTexturedPS(TNORMAL_TANGENT_BINORMAL_TEXTURED_VERTEX_PS IN) : COLOR
{
	float4 l_DiffuseColor = tex2D(S0LinearSampler,IN.UV);
	//************************************************************
	float3 Nn=normalize(IN.WorldNormal);
	
	float3 Tn=normalize(IN.WorldTangent);
	float3 Bn=normalize(IN.WorldBinormal);
	
	float3 bump=g_Bump*(tex2D(S1LinearSampler,IN.UV) - float3(0.5,0.5,0.5));
	Nn = Nn + bump.x*Tn + bump.y*Bn;
	Nn = normalize(Nn);
	//*************************************************************
	
	
	
	//float4 l_LightMap = tex2D(S1LinearSampler,IN.UV2);	
	//l_DiffuseColor=l_DiffuseColor*l_LightMap;
	return CalcLighting(IN.WorldPosition.xyz,Nn.xyz,l_DiffuseColor);
}

technique tecDefaultNormalTanBinTexturedVertex
{
    pass p0
    {        
        VertexShader =compile vs_3_0 RenderNormalTanBinTexturedVS();
        PixelShader = compile ps_3_0 RenderNormalTanBinTexturedPS();
    }
}
//*****************************************************************************************